package emby;

import java.io.File;
import java.io.FileNotFoundException;

import emby.util.IO;

/**
 * The EnvironmentPaths class is a utility class for locating files for the purposes
 * of importing, loading native functions, etc.
 * <br>
 * As emby files may have multiple extensions (.mb, .emby) and programs need to
 * search for them in multiple directories (emby's root directory, the program's working directory, etc),
 * this class is needed to test all possible file locations and name combinations.
 * @author Justin
 */
public class EnvironmentPaths {

	private static String embyDirectory = getEmbyDirectory();
	private static String currentDirectory = null; //working directory

	public static void setWorkingDirectory(String path){
		if( currentDirectory==null){
			currentDirectory = (new File(path)).getAbsolutePath();
		}else{
			throw new InternalError("Working directory already set!");
		}
	}

	/**
	 * Equivalent to setWorkingDirectory, but DO NOT CALL.
	 * This function exists so stdlib.shell's cd command can change working
	 * directories.
	 * @deprecated because only stdlib.shell's cd should call this.
	 * @param path
	 */
	@Deprecated
	public static void cd(String path){
			currentDirectory =  (new File(path)).getAbsolutePath();
	}

	public static String getWorkingDirectory(){
		if(currentDirectory==null){
			return embyDirectory;
			//throw new InternalError("Working directory not set!");
		}
		return currentDirectory;
	}

	/**
	 * If filepath is already a full file path, does nothing.
	 * Else, appends the current working directory to the beginning of the string.
	 * TODO: This method still has bugs.
	 * @param filepath
	 * @return
	 */
	public static String attachWDIfNecc(String filepath){
		//System.out.print("rec: "+filepath+", ");
		if( IO.addSlashIfNecessary(filepath).equals(IO.addSlashIfNecessary(new File(filepath).getAbsolutePath()))){ //TODO
			//System.out.println("passed ");
			return filepath;
		}else{
			//System.out.println("appending!: "+ (new File(filepath)).getAbsolutePath());
			return IO.addSlashIfNecessary(getWorkingDirectory())+filepath;
		}
	}

	/**
	 * Looks for a file in emby's directory, and then in the working directory.
	 * @param filepath
	 * @return
	 * @throws FileNotFoundException
	 */
	public static File findFile(String filepath) throws FileNotFoundException{
		File f = null;
		f = new File(filepath); //Check emby's directory
		if( !f.exists()){
			//check the Lib folder
			String libcheck = IO.addSlashIfNecessary(embyDirectory)+"elib"+System.getProperty("file.separator")+filepath;
			//System.out.println("failed finding "+filepath+"! checking libraries: "+libcheck);
			f = new File(libcheck);
			if(!f.exists()){
				//System.out.println("not in library! checking: "+currentDirectory+filepath);
				f = new File(IO.addSlashIfNecessary(currentDirectory)+filepath);
				if(!f.exists()){
					System.err.println("Working dr: "+currentDirectory);
					System.err.println("Emby dr: "+embyDirectory);
					throw new FileNotFoundException(filepath);
				}
			}
		}
		return f;
	}

	/**
	 * Modifies the filepath to look for files ending in "mb" or "emby"
	 * @param filepath
	 * @return
	 * @throws FileNotFoundException
	 */
	public static File findEmbyFile(String filepath) throws FileNotFoundException{
		String cheese = IO.getFilename(filepath);
		try{
		return findFile(cheese+".mb");
		}catch(FileNotFoundException e){
		return findFile(cheese+".emby");
		}
	}

	private static String getEmbyDirectory(){
		File dir = (new File(System.getProperty("user.dir")));
		if(dir.getAbsolutePath().endsWith("bin") || dir.getAbsolutePath().endsWith("bin\\"))
			return dir.getParent();
		return dir.getAbsolutePath();
	}
}
